<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <div v-if="type == 1">
      <el-pagination
        :background="background"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :layout="layout"
        :page-sizes="pageSizes"
        :total="total"
        v-bind="$attrs"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :hide-on-single-page="false"
      >
      </el-pagination>
    </div>

    <div v-if="type == 2" class="gold-box">
      <el-pagination
        :background="background"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :layout="layout"
        :page-sizes="pageSizes"
        :total="total"
        v-bind="$attrs"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :hide-on-single-page="false"
      >
      </el-pagination>
      <div v-if="totalGold" class="total-gold">
        <span class="total-gold-text">总计金额：</span><span class="total-gold" v-if="">{{totalGold}}元</span>
      </div>
    </div>

  </div>
</template>

<script>
  import {scrollTo} from '@/utils/scroll-to'

  export default {
    name: 'Pagination',
    props: {
      type: {
        type: Number,
        default: 0
      },
      total: {
        required: true,
        type: Number
      },
      page: {
        type: Number,
        default: 1
      },
      limit: {
        type: Number,
        default: 20
      },
      pageSizes: {
        type: Array,
        default() {
          return [20, 50, 100]
        }
      },
      layout: {
        type: String,
        default: 'sizes, prev, pager, next, total, slot'
      },
      background: {
        type: Boolean,
        default: true
      },
      autoScroll: {
        type: Boolean,
        default: true
      },
      hidden: {
        type: Boolean,
        default: false
      },
      totalGold: {
        type: Number,
        default: 0.00
      }
    },
    computed: {
      currentPage: {
        get() {
          return this.page
        },
        set(val) {
          this.$emit('update:page', val)
        }
      },
      pageSize: {
        get() {
          return this.limit
        },
        set(val) {
          this.$emit('update:limit', val)
        }
      }
    },
    methods: {
      handleSizeChange(val) {
        this.$emit('pagination', {page: this.currentPage, limit: val})
        if (this.autoScroll) {
          scrollTo(0, 800)
        }
      },
      handleCurrentChange(val) {
        this.$emit('pagination', {page: val, limit: this.pageSize})
        if (this.autoScroll) {
          scrollTo(0, 800)
        }
      }
    }
  }
</script>

<style scoped>
  .pagination-container {
    text-align: right;
    background: #fff;
    padding: 32px 0px;
    margin-bottom: 24px;
  }

  .pagination-container.hidden {
    display: none;
  }

  .pagination-container >>> .el-pagination__total {
    margin-left: 10px;
  }

  .pagination-container >>> .el-pagination {
    display: flex;
    justify-content: flex-end;
  }

  .pagination-container .total-gold {
    color: #409EFF;
    font-size: 13px;
    font-weight: 400;
    text-align: left;
  }

  .pagination-container .total-gold{
    height: 28px;
    line-height: 28px;
  }
  .pagination-container .total-gold-text {
    color: #606266;
    font-size: 13px;
    font-weight: 400
  }

  .pagination-container .gold-box {
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
</style>
